// Copyright 2010 The Go Example Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"archive/tar"
	"fmt"
	"io"
	"io/ioutil"
	"bytes"
	"os"
)

func main() {
	fileName := "data/data.tar"
	//Open File
	file, err := os.Open(fileName, os.O_RDONLY, 0)
	if file == nil {
		fmt.Printf("can't open file;err=%s\n", err.String())
		os.Exit(1)
	}
	//Close File
	defer file.Close()
	// Create tar.Reader
	tr := tar.NewReader(file)

	for {
		hdr, err2 := tr.Next()

		if err2 != nil {

		}
		if hdr == nil {
			// end of tar archive
			break
		}
		//File Name
		name := hdr.Name

		//Read file data	
		var b bytes.Buffer
		_, err3 := io.Copy(&b, tr)
		if err3 != nil {
			return
		}
		//Write data to File
		ioutil.WriteFile("data/"+name, b.Bytes(), 0444)

	}
}
